
/**
 * @desc 将时间戳转换成具体日期/N天前/N小时前/N分钟前
 * 
 * @param {Number} stamp  时间戳
 * @returns {String} 具体日期/N天前/N小时前/N分钟前
 * 
 * @demo
 *  @input const stamp = 1652941801000
 *  @program fmtTimeTxt(stamp)-> 基于当前时间进行计算的.
 *  @ouput 2022-09-03 13:54:46
 */
function fmtTimeTxt(stamp) {
    debugger
    const minutes = 1000 * 60
    const hours = minutes * 60
    const day = hours * 24
  
    const now = new Date().getTime()
    const diff = now - stamp
  
    if (diff < 0) return ''
  
    // 函数parseInt是用于检索字符串中的整数类型的. 而diff/minutes是number类型.
    const minutesC = Math.floor(diff / minutes)
    const hourC = Math.floor(diff / hours)
    const dayC = Math.floor(diff / day)
  
    if (dayC > 7) return formatDate(now)
    else if (dayC >= 1 && dayC <= 7) return `${dayC}天前`
    else if (hourC >= 1) return `${hourC}小时前`
    else if (minutesC >= 1) return `${minutesC}分钟前`
    else return '刚刚'
}



/**
 * @desc 时间戳转成yyyy-mm-dd hh:mm:ss格式
 * 
 * @param {Number} stamp  时间戳
 * @returns {String} yyyy-mm-dd hh:mm:ss格式时间
 * 
 * @demo
 *  @input const stamp = 1661997699353
 *  @program formatDate(stamp)
 *  @ouput 2022-09-01 10:01:39
 */
function formatDate(stamp) {
    debugger
    if (!stamp) {
        return
    }

    const curr = new Date(stamp)
    const year = curr.getFullYear()
    const month = addZero(curr.getMonth() + 1)
    const day = addZero(curr.getDate())
    const hours = addZero(curr.getHours())
    const minutes = addZero(curr.getMinutes())
    const second = addZero(curr.getSeconds())

    return `${year}-${month}-${day} ${hours}:${minutes}:${second}`
}



// ===================以下代码为黑盒, 不对外暴露START===================
/**
 - 添加0前缀
*/
function addZero(num) {
    return num > 9 ? num : `0${num}`
}
// ===================以下代码为黑盒, 不对外暴露END===================



export {
    fmtTimeTxt,
    formatDate
}

